<?php
class SqlQuery extends Object
{
	public $sql;
	public $params = array();

	public function SqlQuery($sql=null)
	{
		$this->sql = $sql;
	}

	public function Add($key, $value, $type, $list_item_type = null)
	{
		$this->params[$key]['value'] = $value;
		$this->params[$key]['type'] = $type;
		$this->params[$key]['list_item_type'] = $list_item_type;
	}

	public function AddNumber($key, $value)
	{
		$this->Add($key, $value, AppConfig::GetInstance()->SQL_NUMBER);
	}

	public function AddString($key, $value)
	{
		$this->Add($key, $value, AppConfig::GetInstance()->SQL_STRING);
	}

	public function AddList($key, $value, $list_item_type = null)
	{
		$this->Add($key, $value, AppConfig::GetInstance()->SQL_LIST, $list_item_type);
	}

	public function AddStringNoquote($key, $value)
	{
		$this->Add($key, $value, AppConfig::GetInstance()->SQL_STRING_NOQUOTE);
	}

    public function AddBoolean($key, $value)
	{
		$this->Add($key, $value, AppConfig::GetInstance()->SQL_BOOLEAN);
	}

    public function AndCondition($cond)
	{
		if (!is_null($cond))
        {
            $this->sql = empty($this->sql) ? $cond->sql : "({$this->sql}) AND ({$cond->sql})";
            $this->params = array_merge($this->params, $cond->params);
        }
		return $this;
	}

    public function OrCondition($cond)
    {
        if (!is_null($cond))
        {
            $this->sql = empty($this->sql) ? $cond->sql : "({$this->sql}) OR ({$cond->sql})";
            $this->params = array_merge($this->params, $cond->params);
        }
        return $this;
    }

    public function GetValue($key)
	{
		return isset($this->params[$key]) ? $this->params[$key]['value'] : null;
	}

    public function RemoveValue($key)
    {
        if(isset($this->params[$key]))
        {
            unset($this->params[$key]);
            $this->sql = str_replace("$key = @$key", '###', $this->sql);
            $this->sql = str_replace("$key LIKE @$key", '###', $this->sql);
            $this->sql = str_replace('AND ###', null, $this->sql);
            $this->sql = str_replace("OR ###", null, $this->sql);
            $this->sql = str_replace("### AND", null, $this->sql);
            $this->sql = str_replace("### OR", null, $this->sql);
            $this->sql = str_replace("###", null, $this->sql);
        }
    }

    public function SetValue($key, $value)
    {
        if (isset($this->params[$key]))
            $this->params[$key]['value'] = $value;
    }

    public function IsExisted($key)
    {
        return isset($this->params[$key]);
    }

    public function Replace($old, $new)
    {
        $this->sql = str_replace($old, $new, $this->sql);
        if ($this->IsExisted($old))
        {
            $this->params[$new] = $this->params[$old];
            unset($this->params[$old]);
        }
    }

    public function ToggleValue($key)
    {
        $this->SetValue($key, !$this->GetValue($key));
    }
}
?>
